package com.zhxy.service.impl;

import com.github.pagehelper.Constant;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhxy.bean.User;
import com.zhxy.constants.Constants;
import com.zhxy.mapper.UserMapper;
import com.zhxy.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    /**
     * 用户登录
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 查询登录用户
        User user=userMapper.loginByUsername(username);
        if (user==null){
            throw new UsernameNotFoundException("登录用户不存在");
        }
        return user;
    }

    /**
     * 用户分页查询
     * @param current
     * @return
     */
    @Override
    public PageInfo<User> getUserByPage(Integer current) {

        // 设置分页
        PageHelper.startPage(current, Constants.PAGE_SIZE);

        // 查看数据
        List<User> userList=userMapper.selectUserPage();

        PageInfo<User> userPageInfo = new PageInfo<>(userList);
        return userPageInfo;
    }
}
